<template>
  <li>
    <label>
      <!-- 注意: 这里可以使用v-model.也可以实现功能.但是不建议写v-model.因为这样属于修改props数据 -->
      <input type="checkbox" :checked="todo.isDone" @click="handle" />
      <span :class="classObj">{{ todo.todoName }}</span>
    </label>
    <button class="btn btn-danger" style="display: none">删除</button>
  </li>
</template>

<script>
import PubSub from 'pubsub-js'
export default {
  // 接收props数据
  props: ['todo'],
  data() {
    return {
      classObj: {
        done: this.todo.isDone,
      },
    }
  },
  methods: {
    handle() {
      // 通知app,修改指定任务数据的isDone
      PubSub.publish('update', this.todo.id)
    },
  },
  watch: {
    // 监听todo,监听到todo的数据发生变化了,然后修改classObj
    todo: {
      deep: true,
      handler() {
        this.classObj.done = this.todo.isDone
      },
    },
  },
}
</script>

<style>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  display: flex;
  align-items: center;
  /* float: left; */
  cursor: pointer;
}

li label span.done {
  text-decoration: line-through;
  color: #ccc;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
</style>
